<?php
namespace lactone\Core\Models;

use ActiveRecord\Model;

class SpiderModel extends Model{

    static $table = 'spider';

    static $before_create = [
        'inject_time'
    ];

    static $before_save   = [
        'inject_update_time'
    ];

    public $live_update_time;

    /**
     * 插入数据时候自动插入时间
     * @return bool
     */
    public function inject_time(){
        $this->update_at = $this->create_at = time();
        return true;
    }

    /**
     * 更新数据时自动更新
     * @return bool
     */
    public function inject_update_time(){
        $this->update_at = time();
        return true;
    }

    /**
     * 当前蜘蛛是否是活着
     * @return int
     */
    public function get_is_live(){
        return true;
    }

    /**
     * 获取唯一标识符
     */
    public function getUid(){
        return md5($this->ip.$this->port);
    }

    /**
     * 当前蜘蛛是否已经注册
     * @param $ip
     * @param $port
     * @return mixed
     * @throws \ActiveRecord\RecordNotFound
     */
    public static function hasSpider($ip,$port){
        return self::find('first',['ip'=>$ip,'port'=>$port]);
    }

}